package com.wenhai.java.netty;

import com.google.protobuf.InvalidProtocolBufferException;
import com.wenhai.java.netty.coder.protobuf.SubscribeReqProto;

public class TestSubscribeReqProto {

    private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
        return req.toByteArray();
    }

    private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    private static SubscribeReqProto.SubscribeReq createSubScribeReq() {
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqId(1)
                .setUsername("谢文海")
                .setProductName("Protobuf")
                .setAddress("上海");

        return builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubscribeReqProto.SubscribeReq req = createSubScribeReq();
        System.out.println("before encode: " + req.toString());
        SubscribeReqProto.SubscribeReq req2 = decode(encode(req));
        System.out.println("after decode: " + req2.toString());
        System.out.println("req == req2: " + (req2.equals(req)));
    }
}
